local jingchen = fk.CreateSkill{
  name = "investiture__jingchen",
  tags = {"Turn"},
}

Fk:loadTranslationTable{
  ["investiture__jingchen"] = "靖尘",
  [":investiture__jingchen"] = "回合技，一张多目标牌指定所有目标后，若之目标包括你，你可令此牌对其余目标角色无效。若如此做，之结算后，其余目标角色各摸一张牌并交给你或使用者一张牌。",

  ["#investiture__jingchen-invoke"] = "靖尘：你可以令 %arg 对其余目标角色无效",
  ["#investiture__jingchen-give"] = "靖尘：请交给 %src 或 %dest 一张牌",

  ["$investiture__jingchen1"] = "粟撒西疆土，不教烽烟近镐京！",
  ["$investiture__jingchen2"] = "仁者非无刃，止战之刃藏琴匣！",
}

jingchen:addEffect(fk.TargetSpecified, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(jingchen.name) and player:usedSkillTimes(jingchen.name, Player.HistoryTurn) == 0 then
      return data.firstTarget and #data.use:getAllTargets() > 1 and data.use:hasTarget(player)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = jingchen.name,
      prompt = "#investiture__jingchen-invoke:::" .. data.card:toLogString(),
    })
  end,
  on_use = function (self, event, target, player, data)
    data.use.nullifiedTargets = data.use.nullifiedTargets or {}
    table.forEach(data.use:getAllTargets(), function (p)
      if p ~= player then
        table.insertIfNeed(data.use.nullifiedTargets, p)
      end
    end)
    data.extra_data = data.extra_data or {}
    data.extra_data.jingchen_tos = data.use.nullifiedTargets
    data.extra_data.jingchen_invoker = player
  end
})

jingchen:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(jingchen.name, true, true) then
      return data.extra_data and data.extra_data.jingchen_invoker == player and not (player.dead and target.dead)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = data.extra_data.jingchen_tos
    if not tos or #tos == 0 then return end
    room:sortByAction(tos)
    for _, p in ipairs(tos) do
      if player.dead and target.dead then break end
      if not p.dead then
        p:drawCards(1, jingchen.name)
        room:askToYiji(p, {
          min_num = 1,
          max_num = 1,
          targets = table.filter({player, target}, function (e) return not e.dead end),
          prompt = "#investiture__jingchen-give:" .. player.id .. ":" .. target.id,
          skill_name = jingchen.name,
          cancelable = false,
        })
      end
    end
  end
})

return jingchen